package com.obrempon.credit.market.ui.dialog

import android.content.Context
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonDialog
import com.obrempon.credit.market.databinding.ObremponAuthStepWalletAccountSubmitOnlyOneDialogBinding
import com.obrempon.credit.market.event.ObremponEventPointType

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/21 13:45
 * @Annotation：提交只有一个收款账户提示dialog
 */
class ObremponWalletAccountSubmitOnlyOneDialog: CommonDialog {
    private lateinit var binding: ObremponAuthStepWalletAccountSubmitOnlyOneDialogBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeObremponRootView(): View {
        binding = ObremponAuthStepWalletAccountSubmitOnlyOneDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initObremponView() {
        val showOnlyOneKey = getString(R.string.obrempon_auth_step_wallet_account_only_one_tips_2_comptes)
        val showOnlyOneTipsText = String.format(getString(R.string.obrempon_auth_step_wallet_account_only_one_tips), showOnlyOneKey)
        val onlyOneTipsSpannable = SpannableString(showOnlyOneTipsText)
        val startIndex = showOnlyOneTipsText.indexOf(showOnlyOneKey)
        onlyOneTipsSpannable.setSpan(ForegroundColorSpan(getColor(R.color.color_ffa4690a)), startIndex, startIndex + showOnlyOneKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        onlyOneTipsSpannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + showOnlyOneKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textWalletAccountOnlyOneTips.text = onlyOneTipsSpannable

        val showOnlyOneProposalKey = getString(R.string.obrempon_auth_step_wallet_account_only_one_proposal_key)
        val showOnlyOneProposalText = String.format(getString(R.string.obrempon_auth_step_wallet_account_only_one_proposal),showOnlyOneProposalKey)
        val onlyOneProposalSpannable = SpannableString(showOnlyOneProposalText)
        val startProposalIndex = showOnlyOneProposalText.indexOf(showOnlyOneProposalKey)
        onlyOneProposalSpannable.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff0000)), startProposalIndex, startProposalIndex + showOnlyOneProposalKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        onlyOneProposalSpannable.setSpan(StyleSpan(Typeface.BOLD), startProposalIndex, startProposalIndex + showOnlyOneProposalKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textWalletAccountOnlyOneProposal.text = onlyOneProposalSpannable
    }

    override fun initObremponListener() {
        setOnClickListener(binding.buttonWalletWarningAdd) //继续添加
        setOnClickListener(binding.buttonWalletWarningNoMore) //没有更多
        setOnClickListener(binding.imageWalletAccountOnlyOneClose)//关闭
    }

    override fun takeObremponData() {
    }

    override fun onObremponViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.buttonWalletWarningNoMore -> {
                addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_USER_ACCOUNT_SUBMIT_ONLYONE_TIPS_CANCEL,eventRemark)
            }
            R.id.buttonWalletWarningAdd -> {
                addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_USER_ACCOUNT_SUBMIT_ONLYONE_TIPS_CONFIRM,eventRemark)
            }
        }
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_ACCOUNT_SUBMIT_ONLY_ONE_TIPS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_ACCOUNT_SUBMIT_ONLY_ONE_TIPS_DISMISS,eventRemark)
    }
}